﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace OpenRPOSBackOffice
{
    public partial class Account : Page
    {
        public Account()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        public void init()
        {
            this.IsEnabled = true;
            // Retrieve information for current account
            if (Globals.currentEmployee != null)
            {
                // Display employee information
                lblFirstName.Content = Globals.currentEmployee.col_FirstName;
                lblMiddleInitial.Content = Globals.currentEmployee.col_MiddleInitial;
                lblLastName.Content = Globals.currentEmployee.col_LastName;
                tbStreetAddress.Text = Globals.currentEmployee.col_Address1;
                tbAdditionalAddress.Text = Globals.currentEmployee.col_Address2;
                tbCity.Text = Globals.currentEmployee.col_City;
                tbState.Text = Globals.currentEmployee.col_State;
                tbZip.Text = Globals.currentEmployee.col_Zip;
                tbHomePhone.Text = Regex.Replace(Globals.currentEmployee.col_HomePhone, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
                tbCellPhone.Text = Regex.Replace(Globals.currentEmployee.col_CellPhone, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
                tbEmail.Text = Globals.currentEmployee.col_Email;
                lblSSN.Content = Regex.Replace(Globals.currentEmployee.col_SSN, @"(\d{3})(\d{2})(\d{4})", "$1-$2-$3");
            }
            else
            {
                // Clear all of the fields
                lblFirstName.Content = "";
                lblMiddleInitial.Content = "";
                lblLastName.Content = "";
                tbStreetAddress.Text = "";
                tbAdditionalAddress.Text = "";
                tbCity.Text = "";
                tbState.Text = "";
                tbZip.Text = "";
                tbHomePhone.Text = "";
                tbCellPhone.Text = "";
                tbEmail.Text = "";
                lblSSN.Content = "";
            }
            // Ensure personal information is not being displayed by default
            expPersonalInformation.IsExpanded = false;
            Globals.account = this;
        }

        private void btnChangePassword_Click(object sender, RoutedEventArgs e)
        {
            // Prompt the user to update their password....
            ChangePasswordDialog cpd = new ChangePasswordDialog();
            cpd.Show();
        }

        private async void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            // Confirm the update
            if (MessageBox.Show("Are you sure you want to update your information?", "Confirm", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                // Update modified fields
                Globals.showWaitCursor(true);
                // Check street address
                if (Globals.currentEmployee.col_Address1 != tbStreetAddress.Text)
                {
                    Globals.currentEmployee.bFilterAddress1 = true;
                    Globals.currentEmployee.col_Address1 = tbStreetAddress.Text;
                }
                else
                    Globals.currentEmployee.bFilterAddress1 = false;
                // Check additional address
                if (Globals.currentEmployee.col_Address2 != tbAdditionalAddress.Text)
                {
                    Globals.currentEmployee.bFilterAddress2 = true;
                    Globals.currentEmployee.col_Address2 = tbAdditionalAddress.Text;
                }
                else
                    Globals.currentEmployee.bFilterAddress2 = false;
                // Check city
                if (Globals.currentEmployee.col_City != tbCity.Text)
                {
                    Globals.currentEmployee.bFilterCity = true;
                    Globals.currentEmployee.col_City = tbCity.Text;
                }
                else
                    Globals.currentEmployee.bFilterCity = false;
                // Check state
                if (Globals.currentEmployee.col_State != tbState.Text)
                {
                    Globals.currentEmployee.bFilterState = true;
                    Globals.currentEmployee.col_State = tbState.Text;
                }
                else
                    Globals.currentEmployee.bFilterState = false;
                // Check zip
                if (Globals.currentEmployee.col_Zip != tbZip.Text)
                {
                    Globals.currentEmployee.bFilterZip = true;
                    Globals.currentEmployee.col_Zip = tbZip.Text;
                }
                else
                    Globals.currentEmployee.bFilterZip = false;
                // Check home phone
                if (Globals.currentEmployee.col_HomePhone != tbHomePhone.Text)
                {
                    Globals.currentEmployee.bFilterHomePhone = true;
                    Globals.currentEmployee.col_HomePhone = Globals.reDigits.Replace(tbHomePhone.Text,"");
                    tbHomePhone.Text = Regex.Replace(Globals.currentEmployee.col_HomePhone, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
                }
                else
                    Globals.currentEmployee.bFilterHomePhone = false;
                // Check cell phone
                if (Globals.currentEmployee.col_CellPhone != tbCellPhone.Text)
                {
                    Globals.currentEmployee.bFilterCellPhone = true;
                    Globals.currentEmployee.col_CellPhone = Globals.reDigits.Replace(tbCellPhone.Text,"");
                    tbCellPhone.Text = Regex.Replace(Globals.currentEmployee.col_CellPhone, @"(\d{3})(\d{3})(\d{4})", "($1) $2-$3");
                }
                else
                    Globals.currentEmployee.bFilterCellPhone = false;
                // Check e-mail
                if (Globals.currentEmployee.col_Email != tbEmail.Text)
                {
                    Globals.currentEmployee.bFilterEmail = true;
                    Globals.currentEmployee.col_Email = tbEmail.Text;
                }
                else
                    Globals.currentEmployee.bFilterEmail = false;
                // Perform the update...
                await Globals.db.UpdateEmployeeObject(Globals.currentEmployee);
                // Prompt the user
                MessageBox.Show("Information has been updated.");
                Globals.showWaitCursor(false);
            }

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            init();
        }

    }
}
